/**
 * Title :	jFamilyTree generator
 * Description: jFamilyTree generator
 * Version:	provider
 * Copyright (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.dtd;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;

/**
 * DTD
 * 
 * @author Bernard Bou
 */
public class Dtd
{
	/**
	 * DTD file name
	 */
	static final String theDTDFile = "jFamilyTree.dtd"; //$NON-NLS-1$

	/**
	 * Get DTD as string
	 * 
	 * @return DTD as string
	 */
	static public String getString()
	{
		ByteArrayOutputStream thisByteArrayOutputStream = new ByteArrayOutputStream();
		BufferedOutputStream thisOutputStream = new BufferedOutputStream(thisByteArrayOutputStream);
		if (copyToStream(thisOutputStream))
		{
			return thisByteArrayOutputStream.toString();
		}
		return null;
	}

	/**
	 * Copy DTD to output stream
	 * 
	 * @param thisOutputStream
	 *        output stream
	 * @return true if successful
	 */
	static public boolean copyToStream(OutputStream thisOutputStream)
	{
		URL thisURL = Dtd.class.getResource(theDTDFile);
		if (thisURL == null)
		{
			return false;
		}
		try
		{
			InputStream thisInputStream = thisURL.openStream();
			return copyStreams(thisInputStream, thisOutputStream);
		}
		catch (IOException thisException)
		{
			System.err.println("Can't find " + thisURL + " " + thisException.toString()); //$NON-NLS-1$ //$NON-NLS-2$
			return false;
		}
	}

	/**
	 * Copy DTD to UTF8 output stream
	 * 
	 * @param thisOutstream
	 *        output stream
	 * @return true if successful
	 */
	static public boolean copyToUTF8Stream(OutputStream thisOutstream)
	{
		String thisString = getString();
		if (thisString == null)
		{
			return false;
		}
		try
		{
			PrintWriter theWriter = new PrintWriter(new OutputStreamWriter(thisOutstream, "UTF8"), false); //$NON-NLS-1$
			theWriter.print(thisString);
			theWriter.close();
			return true;
		}
		catch (Exception e)
		{
		}
		return false;
	}

	/**
	 * Copy input stream to output stream
	 * 
	 * @param thisInstream
	 *        input stream
	 * @param thisOutstream
	 *        output stream
	 * @return true if successful
	 */
	static private boolean copyStreams(InputStream thisInstream, OutputStream thisOutstream)
	{
		try
		{
			final int thisBufferCount = 512;
			BufferedInputStream theReader = new BufferedInputStream(thisInstream, thisBufferCount);
			BufferedOutputStream theWriter = new BufferedOutputStream(thisOutstream);
			byte thisBuffer[] = new byte[thisBufferCount];
			int thisCount;
			while ((thisCount = theReader.read(thisBuffer, 0, thisBufferCount)) != -1)
			{
				theWriter.write(thisBuffer, 0, thisCount);
			}
			theReader.close();
			theWriter.close();
			return true;
		}
		catch (IOException unused)
		{
		}
		return false;
	}
}
